﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace mijiaxueche.weixin.help
{
    /// <summary>
    /// 使用XML存储accessToken和jsapi_ticket
    /// accessToken
    /// accessToken_time
    /// jsapi_ticket
    /// jsapi_ticket_time
    /// </summary>
    internal class XMLCache
    {
        private static object obj = new object();

        /// <summary>
        /// 缓存文件的路径
        /// </summary>
        private static readonly string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "/xml/";

        private static readonly string xmlFileName = "weixincache.log";

        /// <summary>
        /// 根据路径获取XML文件的内容
        /// </summary>
        /// <param name="xmlPath"></param>
        /// <returns></returns>
        public static DataSet GetXML()
        {
            if (System.IO.File.Exists(xmlPath+xmlFileName))
            {
                DataSet ds = new DataSet();
                ds.ReadXml(xmlPath, XmlReadMode.ReadSchema);
                if (ds.Tables.Count > 0)
                {
                    return ds;
                }
            }
            else
            {
                if (!System.IO.Directory.Exists(xmlPath))
                {
                    System.IO.Directory.CreateDirectory(xmlPath);
                }
                SetXML();
            }
            return null;
        }

        private static void SetXML()
        {
            lock (obj)
            {
                try
                {
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
                    dt.Columns.Add("accessToken");
                    dt.Columns.Add("accessToken_time");
                    dt.Columns.Add("jsapi_ticket");
                    dt.Columns.Add("jsapi_ticket_time");
                    DataRow dr = dt.NewRow();
                    dr["accessToken"] = "";
                    dr["accessToken_time"] = "";
                    dr["jsapi_ticket"] = "";
                    dr["jsapi_ticket_time"] = "";
                    dt.Rows.Add(dr);
                    ds.Tables.Add(dt);
                    ds.WriteXml(xmlPath, XmlWriteMode.WriteSchema);
                }
                catch
                { }
            }
        }

        public static DataView GetXMLView(string strWhere, string strSort)
        {
            DataSet ds = GetXML();
            DataView dv = new DataView(ds.Tables[0]);
            if (strWhere != null)
            {
                dv.RowFilter = strWhere;
            }
            if (strSort != null)
            {
                dv.Sort = strSort;
            }
            return dv;
        }


        public static bool DeleteXml(int rowIndex)
        {
            try
            {
                DataSet ds = GetXML();
                ds.Tables[0].Rows[rowIndex].Delete();
                ds.WriteXml(xmlPath, XmlWriteMode.WriteSchema);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 根据行和列更新XML
        /// </summary>
        /// <param name="xmlPath">绝对路径</param>
        /// <param name="rows">行数</param>
        /// <param name="colname">列名</param>
        /// <param name="Text">更新的内容</param>
        /// <returns></returns>
        public static bool UpdateXml(int rows, string colname, string Text)
        {
            try
            {
                lock (obj)
                {
                    DataSet ds = GetXML();
                    ds.Tables[0].Rows[rows][colname] = Text;
                    ds.Tables[0].Rows[rows][colname + "_time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                    ds.WriteXml(xmlPath, XmlWriteMode.WriteSchema);
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }


        }

        public static string GetValue(string key)
        {
            DataSet ds = GetXML();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][key] == null
                    || ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][key + "_time"] == null
                    || ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][key].ToString() == ""
                    || ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][key + "_time"].ToString() == "")
                {
                    return null;
                }
                if (Convert.ToDateTime(ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][key + "_time"].ToString()).AddHours(2) >
                    DateTime.Now)
                    return ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1][key].ToString();
                else
                    return null;
            }
            else
            {
                return null;
            }

        }
    }
}